﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedComponents;

namespace OurLeague.Object
{
    public class Player : Person
    {
        #region Fields

        String _role;
        String _teamId;

        int _shirtNumber;
        int _foot;
        int _height;
        int _weight;
        int _cards;
        int _status;

        public static List<Player> Players = new List<Player>();

        #endregion

        #region Constructors

        public Player() : base() { }
        public Player(Person person) : base(person) { }

        #endregion

        #region Public Methods

        /*
        public void getScoredGoals()
        {

        }
        */ 
        
        public void getYellowCard()
        {
            _cards++;
        }

        public void getRedCard()
        {
            _cards += 4;
        }

        public new bool delete()
        {
            if (!base.delete())
                return false;

            Players.Remove(this);
            return true;
        }

        public new void save()
        {
            base.save();
            MySqlRecord updateRecord = new MySqlRecord();

            updateRecord["status"] = Status;
            updateRecord["foot"] = Foot;
            updateRecord["height"] = Height;
            updateRecord["weight"] = Weight;
            updateRecord["cards"] = Cards;
            updateRecord["role"] = Role;
            updateRecord["shirtNumber"] = ShirtNumber;
            updateRecord["fkTeamId"] = TeamId;

            if (!Players.Exists(p => p.Id == Id))
            {
                Players.Add(this);
                updateRecord["playerId"] = int.Parse(Id);

                MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Players", null, updateRecord);
                
                return;
            }

            MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Players", "playerId=" + Id, updateRecord);
        }

        /// <summary>
        /// Controlla se il giocatore è disponibile
        /// </summary>
        /// <returns>True se disponibile, false altrimenti</returns>
        public bool isAvailable()
        {
            if (this.Status == 0 & this.Cards < 3)
                return true;
            return false;
        }

        public bool isSentOff(String matchId)
        {
            return Event.Events.Exists(e => e.MatchId == matchId && e.Player.Id == Id && e.Type == 3);
        }

        public bool isOut(String matchId)
        {
            return Event.Events.Exists(e => e.MatchId == matchId && e.Player.Id == Id && e.Type == 5);
        }

        public bool isIn(String matchId)
        {
            return Event.Events.Exists(e => e.MatchId == matchId && e.Player.Id == Id && e.Type == 4);
        }

        public bool hasYellow(String matchId)
        {
            return Event.Events.Exists(e => e.MatchId == matchId && e.Player.Id == Id && e.Type == 2);
        }

        public bool hasRed(String matchId)
        {
            return Event.Events.Exists(e => e.MatchId == matchId && e.Player.Id == Id && e.Type == 3);
        }

        public void getInjury(int days)
        {
            _status = days;
        }

        public void setCardsAndStatus()
        {
            //riduco le giornate di infortunio
            if (_status > 0)
                _status--;
            //sconto l'espulsione
            if (_cards >= 4)
                _cards -= 4;
            //sconto la diffida
            if (_cards == 3)
                _cards = 0;
        }

        #endregion

        #region Properties

        public String Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public String TeamId
        {
            get { return _teamId; }
            set { _teamId = value; }
        }

        public String TeamName
        {
            get { return (Team.load(TeamId)).Name; }
        }

        public int ShirtNumber
        {
            get { return _shirtNumber; }
            set { _shirtNumber = value; }
        }

        public int Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public int Cards
        {
            get { return _cards; }
            set { _cards = value; }
        }

        public int Foot
        {
            get { return _foot; }
            set { _foot = value; }
        }

        public int Weight
        {
            get { return _weight; }
            set { _weight = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public bool isRegular
        {
            get;
            set;
        }

        public System.Drawing.Image LineUpIcon
        {
            get {
                if (isRegular)
                    return OurLeague.Properties.Resources.regular;

                return OurLeague.Properties.Resources.reserve;
            }
        }

        #endregion

        #region Static methods

        public new static void loadAll()
        {
            var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Players", null, null);
            foreach (MySqlRecord playerRecord in queryResult)
            {
                if (Players.Find(p => p.Id == playerRecord["playerId"].ToString()) == null)
                    addNewPlayer(playerRecord);
            }
        }

        public new static Player load(String idPlayer)
        {
            Player player = Players.Find(p => p.Id == idPlayer);

            if (player == null)
            {
                var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Players", null, "playerId=" + idPlayer);
                player = addNewPlayer(queryResult[0]);
            }

            return player;
        }

        public static void removeAll(Team team)
        {
            Players.RemoveAll(p => p.TeamId == team.Id);
            Persons.RemoveAll(p => p.GetType() == typeof(Player) && ((Player)p).TeamId == team.Id);
        }

        private static Player addNewPlayer(MySqlRecord playerRecord)
        {
            Person person = Person.load(playerRecord["playerId"].ToString());
            Player player = new Player(person);
            Persons[Persons.FindIndex(P => P.Id == player.Id)] = player;

            player.Status = int.Parse(playerRecord["status"].ToString());
            player.Foot = int.Parse(playerRecord["foot"].ToString());
            player.Height = int.Parse(playerRecord["height"].ToString());
            player.Weight = int.Parse(playerRecord["weight"].ToString());
            player.Cards = int.Parse(playerRecord["cards"].ToString());
            player.Role = playerRecord["role"].ToString();
            player.TeamId= playerRecord["fkTeamId"].ToString();
            player.ShirtNumber = int.Parse(playerRecord["shirtNumber"].ToString());

            Players.Add(player);

            return player;
        }

        #endregion
    }

    #region Support Class

    #region Scorer

    public class Scorer
    {
        Player _player;
        int _goalNumber;
        String _team;

        public Scorer(Player player, int goalNumber, String team)
        {
            _player = player;
            _goalNumber = goalNumber;
            _team = team;
        }

        public void increaseGoalNumber()
        {
            _goalNumber++;
        }

        public Player Player
        {
            get { return _player; }
            set { _player = value; }
        }

        public String PlayerName
        {
            get { return _player.Surname + " " + _player.Name; }
        }

        public int GoalNumber
        {
            get { return _goalNumber; }
            set { _goalNumber = value; }
        }

        public String Team
        {
            get { return _team; }
        }
    }

    #endregion

    #region Booked

    public class Booked
    {
        Player _player;
        int _bookingNumber;
        String _team;

        public Booked(Player player, int bookingNumber, String team)
        {
            _player = player;
            _bookingNumber = bookingNumber;
            _team = team;
        }

        public void incYellowCardN()
        {
            _bookingNumber++;
        }

        public Player Player
        {
            get { return _player; }
            set { _player = value; }
        }

        public String PlayerName
        {
            get { return _player.Surname + " " + _player.Name; }
        }

        public int BookingNumber
        {
            get { return _bookingNumber; }
            set { _bookingNumber = value; }
        }

        public String Team
        {
            get { return _team; }
        }
    }

    #endregion

    #region Expelled

    public class Expelled
    {
        Player _player;
        int _expelletionNumber;
        String _team;

        public Expelled(Player player, int expelletionNumber, String team)
        {
            _player = player;
            _expelletionNumber = expelletionNumber;
            _team = team;
        }

        public void incRedCardN()
        {
            _expelletionNumber++;
        }

        public Player Player
        {
            get { return _player; }
            set { _player = value; }
        }

        public String PlayerName
        {
            get { return _player.Surname + " " + _player.Name; }
        }

        public int ExpelletionNumber
        {
            get { return _expelletionNumber; }
            set { _expelletionNumber = value; }
        }

        public String Team
        {
            get { return _team; }
        }
    }

    #endregion

    #region PlayedTime

    public class PlayTime
    {
        Player _player;
        int _minutePlayed;
        String _team;

        public PlayTime(Player player, int minutePlayed, String team)
        {
            _player = player;
            _minutePlayed = minutePlayed;
            _team = team;
        }

        public void addMinutes(int minutes)
        {
            _minutePlayed += minutes;
        }

        public Player Player
        {
            get { return _player; }
            set { _player = value; }
        }

        public String PlayerId
        {
            get { return _player.Id; }
        }

        public String PlayerName
        {
            get { return _player.Surname + " " + _player.Name; }
        }

        public int TimePlayed
        {
            get { return _minutePlayed; }
            set { _minutePlayed = value; }
        }

        public String Team
        {
            get { return _team; }
        }
    }

    #endregion

    #endregion
}
